package com.boyadegroot.divelog.ui.diver.tabs;

import android.content.Context;
import android.database.Cursor;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.View;
import android.widget.TextView;

import com.boyadegroot.divelog.R;
import com.boyadegroot.divelog.data.DiveLogData;
import com.boyadegroot.divelog.data.DiveLogDatabaseContract;
import com.boyadegroot.divelog.models.DiveCountry;

public class DiverLocationsAdapter extends SimpleCursorAdapter {

    public DiverLocationsAdapter(Context context, int layout) {
        super(context, layout, null, new String[0], new int[0], 0);
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        String locationName = cursor.getString(cursor.getColumnIndex(DiveLogDatabaseContract.DiveLocations.COLUMN_NAME_NAME));
        long countryId = cursor.getLong(cursor.getColumnIndex(DiveLogDatabaseContract.DiveLocations.COLUMN_NAME_COUNTRY_ID));
        DiveLogData data = DiveLogData.getInstance(context);
        DiveCountry country = data.getDiveCountry(countryId);
        TextView locationView = (TextView) view.findViewById(R.id.locationname);
        TextView countryView = (TextView) view.findViewById(R.id.countryname);
        locationView.setText(locationName);
        countryView.setText(String.format("(%s)", country.getName()));
        super.bindView(view, context, cursor);
    }
}
